import pandas as pd

class ImageFeatureExtraction:
    def __init__(self, file_path: str):
        """
        初始化 ImageFeatureExtraction 类

        :param file_path: 输入的 CSV 文件路径
        """
        self.file_path = file_path

    def analyze_csv(self):
        """
        分析 CSV 文件并生成汇总结果
        """
        try:
            # 读取 CSV 文件
            df = pd.read_csv(self.file_path)

            # 计算总样本数
            total_samples = len(df)

            # 计算 area 列的方差
            area_variance = df['area'].var()

            # 计算 Predicted Label 的数量和占比
            label_counts = df['Predicted_Label'].value_counts()
            proportion_counts = df['Predicted_Label'].value_counts(normalize=True)

            # 获取 Predicted Label 为 0、1、2 的数量和占比
            count_0 = label_counts.get(0, 0)  # 如果没有 0 则返回 0
            count_1 = label_counts.get(1, 0)  # 如果没有 1 则返回 0
            count_2 = label_counts.get(2, 0)  # 如果没有 2 则返回 0

            proportion_0 = proportion_counts.get(0, 0)  # 如果没有 0 则返回 0
            proportion_1 = proportion_counts.get(1, 0)  # 如果没有 1 则返回 0
            proportion_2 = proportion_counts.get(2, 0)  # 如果没有 2 则返回 0

            # 创建一个汇总 DataFrame
            summary_df = pd.DataFrame({
                'Total Samples': [total_samples],
                'Area Variance': [area_variance],
                'Proportion of 0': [proportion_0],
                'Proportion of 1': [proportion_1],
                'Proportion of 2': [proportion_2],
                'Count of 0': [count_0],
                'Count of 1': [count_1],
                'Count of 2': [count_2],
            })
            return summary_df

        except Exception as e:
            print(f"分析 CSV 文件时出错: {str(e)}")
            return None